package net.lwf.wp.fake.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.lwf.jaxb.service.JaxbService;
import net.lwf.wp.fake.jaxb.Data;
import net.lwf.wp.fake.jaxb.Type;
import net.lwf.wp.fake.jaxb.Wordproblems;

public class MockWpDataService extends JaxbService {
	
	private String grade;
	private static final String CONTEXT_PATH = "net.lwf.wp.fake.jaxb";
	private String xmlFileName = "wp/wordproblems-";
	private String fileExtension = ".xml";
	
	private static Map<String, List<Data>> wpDataMap;
	
	public List<Data> getMockData(String in_grade, String type) {
		this.grade = in_grade;
		String key = in_grade + type;
		if(wpDataMap == null) {
			wpDataMap = new HashMap<String, List<Data>>();
		}
		if(!wpDataMap.containsKey(key)) {
			this.init(type);
		}
		return wpDataMap.get(key);
	}
	
	private void init(String in_type) {
		Wordproblems wproblems = (Wordproblems) loadedXmlData();
		List<Type> types = wproblems.getType();
		for(Type type: types) {
			if(in_type.equalsIgnoreCase(type.getName())) {
				List<Data> problemdata = type.getData();
				wpDataMap.put(this.grade+in_type, problemdata);
				return;
			}
		}
	}

	@Override
	public String getContextPath() {
		return CONTEXT_PATH;
	}

	@Override
	public String getXmlFileName() {
		return xmlFileName + this.grade + fileExtension;
	}
}
